package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.EditorialMaterialsContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MainActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/2
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class EditoriaMaterialsPresenter {
    private EditorialMaterialsContract editorialMaterialsContract;
    private AppCompatActivity me;


    public EditoriaMaterialsPresenter(EditorialMaterialsContract editorialMaterialsContract, AppCompatActivity me) {
        this.editorialMaterialsContract = editorialMaterialsContract;
        this.me = me;
    }

    /**
     * 编辑个人资料
     */
    public void getModBaseData(String token, String nickname, String city, String birthday,
                               String expect_ids, String qq, String wx, String height, String weight, String description
            , String set_hidden_account) {
        if (TextUtils.isEmpty(token)) {
            ToastUtil.show("token不能为空");
            return;
        }
        if (TextUtils.isEmpty(nickname)) {
            ToastUtil.show("昵称不能为空");
            return;
        }
        if (TextUtils.isEmpty(city)) {
            ToastUtil.show("城市不能为空");
            return;
        }
        if (TextUtils.isEmpty(birthday)) {
            ToastUtil.show("请选择生日");
            return;
        }
//        if (TextUtils.isEmpty(expect_ids)) {
//            ToastUtil.show("请选择期望对象");
//            return;
//        }
        Logger.d(token + "\n" + nickname + "\n" + city + "\n" + birthday + "\n" + city + "\n" + birthday + "\n");
        editorialMaterialsContract.showLoading();


        Model.getInstance().getUserApi().getQueryIdbyName(city).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Map<String, String> map = Constants.codeGet(me, response);
                if (map == null) {
                    return;
                }
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                if (data == null) {
                    return;
                }
                Model.getInstance().getUserApi()
                        .getModBaseData(token, nickname,data.get("id"), birthday, expect_ids, qq, wx, height, weight, description, set_hidden_account)
                        .enqueue(new Callback<JsonObject>() {
                            @Override
                            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                                Map<String, String> map = Constants.codeGet(me, response);
                                if (map == null) {
                                    return;
                                }
                                ToastUtil.show(map.get("message"));
                                me.startActivity(new Intent(me, MainActivity.class));
                                me.finish();
                            }

                            @Override
                            public void onFailure(Call<JsonObject> call, Throwable t) {

                            }
                        });
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {

            }
        });

    }

    public void getIndex() {
        editorialMaterialsContract.showLoading();
        Model.getInstance().getUserApi().getIndex()
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        if (list == null) {
                            return;
                        }
                        editorialMaterialsContract.loginSuccess(list);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }
}
